package com.liuqianfei.markme.rules.block;

import com.liuqianfei.markme.core.Token;
import com.liuqianfei.markme.rules.AbstractState;
import com.liuqianfei.markme.rules.IAction;

/**
 * Code block (4 spaces padded)
 * @author liuqianfei
 */
public class CodeAction implements IAction
{
    @Override
    public boolean parse(AbstractState state, boolean silent, int startLine, int endLine)
    {
        int nextLine, last;
        Token token;

        if (state.getsCount(startLine) - state.getBlkIndent() < 4)
            return false;

        last = nextLine = startLine + 1;

        while (nextLine < endLine)
        {
            if (state.isEmpty(nextLine))
            {
                nextLine++;
                continue;
            }

            if (state.getsCount(nextLine) - state.getBlkIndent() >= 4)
            {
                nextLine++;
                last = nextLine;
                continue;
            }
            break;
        }

        state.setLine(last);

        token         = state.push("code_block", "code", 0);
        token.content = state.getLines(startLine, last, 4 + state.getBlkIndent(), true);
        token.map     = new int[]{startLine, state.getLine()};

        return true;
    }
}
